// pages/demand_my/demand_my.js

var service_view = require("../../service/view.js");
var service_user = require("../../service/user.js");
var service_util = require("../../service/util.js");
var service_common = require("../../service/common.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    demandList: [],
    page: 1,
    oid: 0,
    off: false,
    allDemandList: [],
    send_id: 0,
    focus: false
  },
  img_look: function (e) {
    console.log(e)
    wx.previewImage({
      current: e.currentTarget.dataset.arr[e.currentTarget.dataset.index], // 当前显示图片的http链接
      urls: e.currentTarget.dataset.arr // 需要预览的图片http链接列表
    })
  },
  love: function (e) {

    wx.navigateTo({
      url: '/pages/loveList/loveList?id=' + e.currentTarget.dataset.id,
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // setInterval(() => {
    //   this.setData({
    //     off: false
    //   })
    // }, 5000)
    wx.hideShareMenu()
    wx.showLoading({
      title: '加载中',
    })
    this.MyDemandList();

  },
  onShow: function () {

  },
  off: function () {
    this.setData({
      off: false
    })
  },
  // play: function (e) {
  //   var ctx = wx.createVideoContext(e.currentTarget.id)
  //   ctx.requestFullScreen();//进入全屏
  //   console.log(e)
  // },
  end: function (e) {
    var ctx = wx.createVideoContext(e.currentTarget.id)
    ctx.exitFullScreen();//退出全屏
  },
  MyDemandList: function () {
    wx.showLoading({
      title: '数据加载中',
    })
    service_view.getMyDemandList(this.data.page, this.demandData);
  },
  //格式化时间
  format: function () {
    var demandList = this.data.demandList;
    for (var i = 0; i < demandList.length; i++) {
      demandList[i].addTime = service_util.getTime(demandList[i].addTime);
    }
    this.setData({
      demandList: demandList
    })
  },
  //万人圈数据获取
  demandData: function (res) {
    var demandList = res.data.data;
    for (var i = 0; i < demandList.length; i++) {
      demandList[i].addTime = service_util.getTime(demandList[i].addTime)
      this.data.allDemandList.push(demandList[i]);
    }

    //更新数据
    this.setData({
      demandList: this.data.allDemandList
    })
    wx.hideLoading();
    wx.stopPullDownRefresh();
  },
  // 删除某条评论
  del_l: function (e) {
    service_common.fetchData(
      service_common.getUrl("user.deleteReply"),
      {
        openid: getApp().globalData.openid,
        reply_id: e.currentTarget.dataset.id
      },
       (res)=>{
        if (res.data.code == 0) {
          this.onPullDownRefresh()
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
          })
        }
      },
      "GET"
    )
  },
  toDetail: function (e) {
    add_form_id(e.detail.formId)
    console.log(e)
    var openid = e.currentTarget.dataset.openid;
    wx.navigateTo({
      url: '/pages/new_demand_detail/new_demand_detail?openid=' + openid + '&id=' + e.currentTarget.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  onPullDownRefresh: function () {
    this.data.page = 1
    this.data.allDemandList = []
    this.MyDemandList();
  },
  onReachBottom: function () {
    this.data.page++;
    this.MyDemandList();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      title: "'" + getApp().globalData.userInfo.nickname + "'" + ",分享了一条动态",
      path: '/pages/demand_detail/demand_detail?id=' + res.target.id,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  setLike: function (e) {
    console.log(e)
    service_view.TopicLove(e.currentTarget.dataset.id, this.stateLike);
  },
  stateLike: function (res) {
    console.log(res.data)
    var that = this
    if (res.data.code == 0) {
      wx.showToast({
        title: '点赞成功',
      })
      this.setData({
        page: 1,
        allUserList: [],
        searchUserList2: [],
        allDemandList: [],
        off: false
      })
      wx.hideShareMenu()
      wx.showLoading({
        title: '加载中',
      })
      this.MyDemandList();
    } else {
      wx.showToast({
        title: '已点赞',
      })
    }
  },
  ed: function () {

  },


  //点击查看名片
  viewCard: function (e) {
    add_form_id(e.detail.formId);
    var openid = getApp().globalData.userInfo.openid

    wx.navigateTo({
      url: '/pages/other/other?openid=' + openid,
    })
  },

  //点击弹出操作栏 靠谱 评论 联系
  showOperation: function (e) {
    if (e.currentTarget.dataset.index == this.data.oid) {
      this.setData({
        off: !this.data.off
      })
      return
    } else {
      this.setData({
        off: false
      })
    }
    this.setData({
      oid: e.currentTarget.dataset.index,
      off: true
    })


  },
  modalHide: function (e) {
    var target_id = e.target.id;
    if (target_id == '') {
      this.setData({
        modal: '',
        focus: false
      })
    }
  },
  //点击展开评论模态框
  modalShow: function (e) {
    this.setData({
      focus: true,
      modal: 'show',
      showOperation: '',
      send_id: e.currentTarget.dataset.id
    })
  },

  formSubmit: function (e) {

    var content = e.detail.value.content

    service_view.TopicReply(this.data.send_id, content, this.onFormSubmit);
    this.setData({
      val: ""
    })
  },
  onFormSubmit: function (res) {
    var that = this;
    if (res.data.code == 0) {
      that.setData({
        modal: '',
        showOperation: "",
        page: 1,
        replyList: [],
        reply: [],
        off: false,

      })
      wx.showToast({
        title: '评论成功',
        icon: 'success',
        duration: 1000
      })
      this.onPullDownRefresh()
    } else {
      wx.showModal({
        title: '提示',
        content: res.data.message,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
          that.setData({
            off: false
          })
        }

      })
    }
  },
  Delete: function (e) {
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: res => {
        if (res.confirm) {
          service_view.wanrenquanDelete(e.currentTarget.id, this.onDelete);
        }

      }
    })
  },
  onDelete: function (res) {
    if (res.data.code == 0) {
      wx.showToast({
        title: '删除成功',
      })
      this.setData({
        page: 1,
        allDemandList: [],
        off: false
      })
      this.MyDemandList();

    } else {
      wx.showModal({
        title: '提示',
        content: res.data.message,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  }
})
//添加form_id
function add_form_id(form_id) {
  service_user.addFormId(form_id);
}